﻿#region DaSampleKept
/*----------------------------------------------------------------
// 文件名：DaSampleKept.cs
// 功能描述：表dbo.Sample_Kept的数据访问层
//
// 
// 创建时间：2009-11-20 DtataAccess template . Ver 5.0.20090413
//
// 修改标识： 
// 修改描述： 
//----------------------------------------------------------------*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Globalization;
using System.Text;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

using EQT.Model;


namespace EQT.Dal
{
    ///可以继承更上层接口,ISampleKept ,数据访问层基类DaBase，
    ///DaBase中包含了DbException异常处理静态方法
    public partial class DaSampleKept : DaBase<MoSampleKept>
    {
        #region SQL const
        internal const string TABLE_COLUMNS = " SAM_KEP_ID ,RECEIVER ,SENDER ,SENDER_TEL ,SENDER_CORP ,SAMPLE_NAME ,MODEL ,QTY ,SAMPLE_STATUS ,RECEIVE_DATE ,STORE_ADDR ,REM   ";

        internal const string SQL_INSERT = "INSERT INTO SAMPLE_KEPT (" + TABLE_COLUMNS + ") VALUES (@SamKepId,@Receiver,@Sender,@SenderTel,@SenderCorp,@SampleName,@Model,@Qty,@SampleStatus,@ReceiveDate,@StoreAddr,@Rem)";
        internal const string SQL_SELECT = "SELECT " + TABLE_COLUMNS + " FROM SAMPLE_KEPT  ";
        internal const string SQL_SELECT_ONE = SQL_SELECT + " WHERE SAM_KEP_ID=@SamKepId";

        internal const string SQL_EXIST = "SELECT COUNT(*) FROM SAMPLE_KEPT  WHERE SAM_KEP_ID=@SamKepId  ";
        internal const string SQL_UPDATE = "UPDATE SAMPLE_KEPT SET RECEIVER=@Receiver, SENDER=@Sender, SENDER_TEL=@SenderTel, SENDER_CORP=@SenderCorp, SAMPLE_NAME=@SampleName, MODEL=@Model, QTY=@Qty, SAMPLE_STATUS=@SampleStatus, RECEIVE_DATE=@ReceiveDate, STORE_ADDR=@StoreAddr, REM=@Rem WHERE SAM_KEP_ID=@SamKepId";

        internal const string SQL_DELETE_DEFAULT = "DELETE FROM SAMPLE_KEPT ";
        internal const string SQL_DELETE = "DELETE FROM SAMPLE_KEPT WHERE SAM_KEP_ID=@SamKepId";

        internal const string SQL_COUNT = "SELECT COUNT(*) FROM Sample_Kept ";
        #endregion

        #region Constructor

        /// <summary>
        /// 默认构造函数，使用配置文件中默认的数据库配置。
        /// </summary>
        public DaSampleKept()
        {
            this.db = DatabaseFactory.CreateDatabase();
        }
        /// <summary>
        /// 使用指定的databaseName数据库配置。
        /// </summary>
        /// <param name="databaseName">配置文件中数据库的配置名称。</param>
        public DaSampleKept(string databaseName)
        {
            this.db = DatabaseFactory.CreateDatabase(databaseName);
        }

        public DaSampleKept(Database db)
        {
            this.db = db;
        }

        #endregion

        //以下是CRUD方法，每个方法都有引入外部事务的版本

        #region 构造器模块

        internal override MoSampleKept ConstructT()
        {
            return new MoSampleKept();
        }
        #endregion



        #region Add模块 Helper

        ///build the command object.It never throw exception.
        internal override DbCommand ConstructAddCommand(Database db)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_INSERT);

            db.AddInParameter(dbCommand, "SamKepId", DbType.AnsiString);
            db.AddInParameter(dbCommand, "Receiver", DbType.String);
            db.AddInParameter(dbCommand, "Sender", DbType.String);
            db.AddInParameter(dbCommand, "SenderTel", DbType.AnsiString);
            db.AddInParameter(dbCommand, "SenderCorp", DbType.String);
            db.AddInParameter(dbCommand, "SampleName", DbType.String);
            db.AddInParameter(dbCommand, "Model", DbType.String);
            db.AddInParameter(dbCommand, "Qty", DbType.Int32);
            db.AddInParameter(dbCommand, "SampleStatus", DbType.String);
            db.AddInParameter(dbCommand, "ReceiveDate", DbType.DateTime);
            db.AddInParameter(dbCommand, "StoreAddr", DbType.String);
            db.AddInParameter(dbCommand, "Rem", DbType.String);
            return dbCommand;
        }
        internal override DbCommand ConstructAddCommand(Database db, MoSampleKept entity)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_INSERT);
            db.AddInParameter(dbCommand, "SamKepId", DbType.AnsiString, entity.SamKepId);
            db.AddInParameter(dbCommand, "Receiver", DbType.String, entity.Receiver);
            db.AddInParameter(dbCommand, "Sender", DbType.String, entity.Sender);
            db.AddInParameter(dbCommand, "SenderTel", DbType.AnsiString, entity.SenderTel);
            db.AddInParameter(dbCommand, "SenderCorp", DbType.String, entity.SenderCorp);
            db.AddInParameter(dbCommand, "SampleName", DbType.String, entity.SampleName);
            db.AddInParameter(dbCommand, "Model", DbType.String, entity.Model);
            db.AddInParameter(dbCommand, "Qty", DbType.Int32, entity.Qty);
            db.AddInParameter(dbCommand, "SampleStatus", DbType.String, entity.SampleStatus);
            db.AddInParameter(dbCommand, "ReceiveDate", DbType.DateTime, entity.ReceiveDate);
            db.AddInParameter(dbCommand, "StoreAddr", DbType.String, entity.StoreAddr);
            db.AddInParameter(dbCommand, "Rem", DbType.String, entity.Rem);
            return dbCommand;
        }

        internal override DbCommand PopulateAddCommandParameters(Database db, DbCommand addCmd, MoSampleKept entity)
        {
            db.SetParameterValue(addCmd, "SamKepId", entity.SamKepId);
            db.SetParameterValue(addCmd, "Receiver", entity.Receiver);
            db.SetParameterValue(addCmd, "Sender", entity.Sender);
            db.SetParameterValue(addCmd, "SenderTel", entity.SenderTel);
            db.SetParameterValue(addCmd, "SenderCorp", entity.SenderCorp);
            db.SetParameterValue(addCmd, "SampleName", entity.SampleName);
            db.SetParameterValue(addCmd, "Model", entity.Model);
            db.SetParameterValue(addCmd, "Qty", entity.Qty);
            db.SetParameterValue(addCmd, "SampleStatus", entity.SampleStatus);
            db.SetParameterValue(addCmd, "ReceiveDate", entity.ReceiveDate);
            db.SetParameterValue(addCmd, "StoreAddr", entity.StoreAddr);
            db.SetParameterValue(addCmd, "Rem", entity.Rem);
            return addCmd;
        }

        internal override DbCommand PopulateAddCommandParameters(Database db, DbCommand addCmd, DataRow row)
        {
            db.SetParameterValue(addCmd, "SamKepId", row["SamKepId"]);
            db.SetParameterValue(addCmd, "Receiver", row["Receiver"]);
            db.SetParameterValue(addCmd, "Sender", row["Sender"]);
            db.SetParameterValue(addCmd, "SenderTel", row["SenderTel"]);
            db.SetParameterValue(addCmd, "SenderCorp", row["SenderCorp"]);
            db.SetParameterValue(addCmd, "SampleName", row["SampleName"]);
            db.SetParameterValue(addCmd, "Model", row["Model"]);
            db.SetParameterValue(addCmd, "Qty", row["Qty"]);
            db.SetParameterValue(addCmd, "SampleStatus", row["SampleStatus"]);
            db.SetParameterValue(addCmd, "ReceiveDate", row["ReceiveDate"]);
            db.SetParameterValue(addCmd, "StoreAddr", row["StoreAddr"]);
            db.SetParameterValue(addCmd, "Rem", row["Rem"]);
            return addCmd;
        }

        #endregion

        #region Update模块 Helper

        internal override DbCommand ConstructUpdateCommand(Database db)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_UPDATE);
            db.AddInParameter(dbCommand, "Receiver", DbType.String);
            db.AddInParameter(dbCommand, "Sender", DbType.String);
            db.AddInParameter(dbCommand, "SenderTel", DbType.AnsiString);
            db.AddInParameter(dbCommand, "SenderCorp", DbType.String);
            db.AddInParameter(dbCommand, "SampleName", DbType.String);
            db.AddInParameter(dbCommand, "Model", DbType.String);
            db.AddInParameter(dbCommand, "Qty", DbType.Int32);
            db.AddInParameter(dbCommand, "SampleStatus", DbType.String);
            db.AddInParameter(dbCommand, "ReceiveDate", DbType.DateTime);
            db.AddInParameter(dbCommand, "StoreAddr", DbType.String);
            db.AddInParameter(dbCommand, "Rem", DbType.String);
            db.AddInParameter(dbCommand, "SamKepId", DbType.AnsiString);

            return dbCommand;
        }
        internal override DbCommand ConstructUpdateCommand(Database db, MoSampleKept entity)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_UPDATE);
            db.AddInParameter(dbCommand, "Receiver", DbType.String, entity.Receiver);
            db.AddInParameter(dbCommand, "Sender", DbType.String, entity.Sender);
            db.AddInParameter(dbCommand, "SenderTel", DbType.AnsiString, entity.SenderTel);
            db.AddInParameter(dbCommand, "SenderCorp", DbType.String, entity.SenderCorp);
            db.AddInParameter(dbCommand, "SampleName", DbType.String, entity.SampleName);
            db.AddInParameter(dbCommand, "Model", DbType.String, entity.Model);
            db.AddInParameter(dbCommand, "Qty", DbType.Int32, entity.Qty);
            db.AddInParameter(dbCommand, "SampleStatus", DbType.String, entity.SampleStatus);
            db.AddInParameter(dbCommand, "ReceiveDate", DbType.DateTime, entity.ReceiveDate);
            db.AddInParameter(dbCommand, "StoreAddr", DbType.String, entity.StoreAddr);
            db.AddInParameter(dbCommand, "Rem", DbType.String, entity.Rem);
            db.AddInParameter(dbCommand, "SamKepId", DbType.AnsiString, entity.SamKepId);

            return dbCommand;
        }

        internal override DbCommand PopulateUpdateCommandParameters(Database db, DbCommand updateCmd, MoSampleKept entity)
        {
            db.SetParameterValue(updateCmd, "Receiver", entity.Receiver);
            db.SetParameterValue(updateCmd, "Sender", entity.Sender);
            db.SetParameterValue(updateCmd, "SenderTel", entity.SenderTel);
            db.SetParameterValue(updateCmd, "SenderCorp", entity.SenderCorp);
            db.SetParameterValue(updateCmd, "SampleName", entity.SampleName);
            db.SetParameterValue(updateCmd, "Model", entity.Model);
            db.SetParameterValue(updateCmd, "Qty", entity.Qty);
            db.SetParameterValue(updateCmd, "SampleStatus", entity.SampleStatus);
            db.SetParameterValue(updateCmd, "ReceiveDate", entity.ReceiveDate);
            db.SetParameterValue(updateCmd, "StoreAddr", entity.StoreAddr);
            db.SetParameterValue(updateCmd, "Rem", entity.Rem);
            db.SetParameterValue(updateCmd, "SamKepId", entity.SamKepId);
            return updateCmd;
        }
        internal override DbCommand PopulateUpdateCommandParameters(Database db, DbCommand updateCmd, DataRow row)
        {
            db.SetParameterValue(updateCmd, "Receiver", row["Receiver"]);
            db.SetParameterValue(updateCmd, "Sender", row["Sender"]);
            db.SetParameterValue(updateCmd, "SenderTel", row["SenderTel"]);
            db.SetParameterValue(updateCmd, "SenderCorp", row["SenderCorp"]);
            db.SetParameterValue(updateCmd, "SampleName", row["SampleName"]);
            db.SetParameterValue(updateCmd, "Model", row["Model"]);
            db.SetParameterValue(updateCmd, "Qty", row["Qty"]);
            db.SetParameterValue(updateCmd, "SampleStatus", row["SampleStatus"]);
            db.SetParameterValue(updateCmd, "ReceiveDate", row["ReceiveDate"]);
            db.SetParameterValue(updateCmd, "StoreAddr", row["StoreAddr"]);
            db.SetParameterValue(updateCmd, "Rem", row["Rem"]);
            db.SetParameterValue(updateCmd, "SamKepId", row["SamKepId"]);
            return updateCmd;
        }

        internal override void PrepareDataAdapterCommand(Database db, out DbCommand dbInsertCommand,
                             out DbCommand dbUpdateCommand, out DbCommand dbDeleteCommand)
        {
            dbInsertCommand = db.GetSqlStringCommand(SQL_INSERT);
            #region set insert cmd parameters
            db.AddInParameter(dbInsertCommand, "SamKepId", DbType.AnsiString, "SAM_KEP_ID", DataRowVersion.Current);
            db.AddInParameter(dbInsertCommand, "Receiver", DbType.String, "RECEIVER", DataRowVersion.Current);
            db.AddInParameter(dbInsertCommand, "Sender", DbType.String, "SENDER", DataRowVersion.Current);
            db.AddInParameter(dbInsertCommand, "SenderTel", DbType.AnsiString, "SENDER_TEL", DataRowVersion.Current);
            db.AddInParameter(dbInsertCommand, "SenderCorp", DbType.String, "SENDER_CORP", DataRowVersion.Current);
            db.AddInParameter(dbInsertCommand, "SampleName", DbType.String, "SAMPLE_NAME", DataRowVersion.Current);
            db.AddInParameter(dbInsertCommand, "Model", DbType.String, "MODEL", DataRowVersion.Current);
            db.AddInParameter(dbInsertCommand, "Qty", DbType.Int32, "QTY", DataRowVersion.Current);
            db.AddInParameter(dbInsertCommand, "SampleStatus", DbType.String, "SAMPLE_STATUS", DataRowVersion.Current);
            db.AddInParameter(dbInsertCommand, "ReceiveDate", DbType.DateTime, "RECEIVE_DATE", DataRowVersion.Current);
            db.AddInParameter(dbInsertCommand, "StoreAddr", DbType.String, "STORE_ADDR", DataRowVersion.Current);
            db.AddInParameter(dbInsertCommand, "Rem", DbType.String, "REM", DataRowVersion.Current);
            #endregion
            dbUpdateCommand = db.GetSqlStringCommand(SQL_UPDATE);
            #region Set update cmd value parameters
            db.AddInParameter(dbUpdateCommand, "Receiver", DbType.String, "RECEIVER", DataRowVersion.Current);
            db.AddInParameter(dbUpdateCommand, "Sender", DbType.String, "SENDER", DataRowVersion.Current);
            db.AddInParameter(dbUpdateCommand, "SenderTel", DbType.AnsiString, "SENDER_TEL", DataRowVersion.Current);
            db.AddInParameter(dbUpdateCommand, "SenderCorp", DbType.String, "SENDER_CORP", DataRowVersion.Current);
            db.AddInParameter(dbUpdateCommand, "SampleName", DbType.String, "SAMPLE_NAME", DataRowVersion.Current);
            db.AddInParameter(dbUpdateCommand, "Model", DbType.String, "MODEL", DataRowVersion.Current);
            db.AddInParameter(dbUpdateCommand, "Qty", DbType.Int32, "QTY", DataRowVersion.Current);
            db.AddInParameter(dbUpdateCommand, "SampleStatus", DbType.String, "SAMPLE_STATUS", DataRowVersion.Current);
            db.AddInParameter(dbUpdateCommand, "ReceiveDate", DbType.DateTime, "RECEIVE_DATE", DataRowVersion.Current);
            db.AddInParameter(dbUpdateCommand, "StoreAddr", DbType.String, "STORE_ADDR", DataRowVersion.Current);
            db.AddInParameter(dbUpdateCommand, "Rem", DbType.String, "REM", DataRowVersion.Current);
            #endregion
            #region set update cmd pk where parameters
            db.AddInParameter(dbUpdateCommand, "SamKepId", DbType.AnsiString, "SAM_KEP_ID", DataRowVersion.Current);
            #endregion
            dbDeleteCommand = db.GetSqlStringCommand(SQL_DELETE);
            #region set delete cmd pk where parameters
            db.AddInParameter(dbDeleteCommand, "SamKepId", DbType.AnsiString, "SAM_KEP_ID", DataRowVersion.Current);
            #endregion
        }
        #endregion

        #region Delete模块 Helper

        internal override DbCommand ConstructDeleteCommand(Database db)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_DELETE);

            db.AddInParameter(dbCommand, "SamKepId", DbType.AnsiString);

            return dbCommand;
        }
        internal override DbCommand ConstructDeleteCommand(Database db, MoSampleKept entity)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_DELETE);

            db.AddInParameter(dbCommand, "SamKepId", DbType.AnsiString, entity.SamKepId);

            return dbCommand;
        }

        internal override DbCommand ConstructDeleteCommandForWhere(Database db, string where)
        {
            return db.GetSqlStringCommand(SQL_DELETE_DEFAULT + where);
        }

        internal override DbCommand PopulateDeleteCommandParameters(Database db, DbCommand deleteCmd, MoSampleKept entity)
        {
            db.SetParameterValue(deleteCmd, "SamKepId", entity.SamKepId);
            return deleteCmd;
        }

        internal override DbCommand PopulateDeleteCommandParameters(Database db, DbCommand deleteCmd, DataRow row)
        {
            db.SetParameterValue(deleteCmd, "SamKepId", row["SamKepId"]);
            return deleteCmd;
        }

        #endregion helper

        #region Query模块 Helper

        internal override DbCommand ConstructQueryCommand(string condition)
        {
            return db.GetSqlStringCommand(SQL_SELECT + condition);
        }

        #endregion

        #region GetEntity(s)模块 Helper

        internal override DbCommand ConstructSelectOneCommand(Database db, MoSampleKept entity)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_SELECT_ONE);

            db.AddInParameter(dbCommand, "SamKepId", DbType.AnsiString, entity.SamKepId);
            return dbCommand;
        }

        internal override void PopulateEntityByDataReader(IDataReader reader, ref MoSampleKept entity)
        {
            if (!reader.IsDBNull(0)) entity.SamKepId = reader.GetString(0);
            if (!reader.IsDBNull(1)) entity.Receiver = reader.GetString(1);
            if (!reader.IsDBNull(2)) entity.Sender = reader.GetString(2);
            if (!reader.IsDBNull(3)) entity.SenderTel = reader.GetString(3);
            if (!reader.IsDBNull(4)) entity.SenderCorp = reader.GetString(4);
            if (!reader.IsDBNull(5)) entity.SampleName = reader.GetString(5);
            if (!reader.IsDBNull(6)) entity.Model = reader.GetString(6);
            if (!reader.IsDBNull(7)) entity.Qty = reader.GetInt32(7);
            if (!reader.IsDBNull(8)) entity.SampleStatus = reader.GetString(8);
            if (!reader.IsDBNull(9)) entity.ReceiveDate = reader.GetDateTime(9);
            if (!reader.IsDBNull(10)) entity.StoreAddr = reader.GetString(10);
            if (!reader.IsDBNull(11)) entity.Rem = reader.GetString(11);
        }



        internal override DbCommand ConstructSelectConditionCommand(Database db, string condition)
        {
            return db.GetSqlStringCommand(SQL_SELECT + condition);
        }

        #endregion

        #region Count模块 Helper

        internal override DbCommand ConstructCountCommand(Database db)
        {
            return db.GetSqlStringCommand(SQL_COUNT);
        }

        internal override DbCommand ConstructCountConditionCommand(Database db, string condition)
        {
            return db.GetSqlStringCommand(SQL_COUNT + condition);
        }

        #endregion

        #region IsExist模块 Helper

        internal override DbCommand ConstructIsExistCommand(Database db, MoSampleKept entity)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_EXIST);
            db.AddInParameter(dbCommand, "SamKepId", DbType.AnsiString, entity.SamKepId);
            return dbCommand;
        }

        #endregion



    }
}
#endregion

